package com.ls.pattern.strategy.promotion;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author 挥之以墨
 */
public enum PromotionStrategyFactory {
    /**
     * 促销策略Key及对象
     */
    EMPTY("empty", new EmptyPromotion()),
    CASH_BACK("cashBack", new CashBackPromotion()),
    COUPONS("coupons", new CouponsPromotion()),
    GROUP_BUY("groupBuy", new GroupBuyPromotion());

    private String promotionKey;
    private IPromotionStrategy promotionStrategy;

    private static Map<String, IPromotionStrategy> PROMOTIONS = new HashMap<>();

    static {
        for (PromotionStrategyFactory value : PromotionStrategyFactory.values()) {
            PROMOTIONS.put(value.promotionKey, value.promotionStrategy);
        }
    }

    PromotionStrategyFactory(String promotionKey, IPromotionStrategy emptyPromotion) {
        this.promotionKey = promotionKey;
        this.promotionStrategy = emptyPromotion;
    }


    public static String[] getPromotionKeys() {
        return PROMOTIONS.keySet().toArray(new String[0]);
    }

    public static IPromotionStrategy getStrategyByKey(String promotionKey) {
        return Optional.ofNullable(PROMOTIONS.get(promotionKey))
                .orElse(PromotionStrategyFactory.EMPTY.promotionStrategy);
    }

}
